package com.zcn.io.web.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zcn.io.common.entity.AnswerTemplate;
import com.zcn.io.common.util.DateUtil;
import com.zcn.io.web.mapper.AnswerTemplateMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

import static javax.swing.UIManager.get;

/**
 * @desc:
 * @author: 202438 邱建伟
 * @date: 2024/10/5 18:59
 * @Copyright (c) 2024, All Right Reserved
 */
@Service
@RequiredArgsConstructor
public class AnswerTemplateService {

    private final AnswerTemplateMapper answerTemplateMapper;

    public List<AnswerTemplate> listAll() {
        return answerTemplateMapper.selectList(new LambdaQueryWrapper<>());
    }

    public void save(AnswerTemplate answerTemplate) {
        answerTemplateMapper.insert(answerTemplate);
    }

    public void updateById(AnswerTemplate answerTemplate) {
        answerTemplateMapper.updateById(answerTemplate);
    }

    public void deleteById(String id) {
        answerTemplateMapper.deleteById(id);
    }

    public AnswerTemplate getLatestOne() {
        LambdaQueryWrapper<AnswerTemplate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AnswerTemplate::getEffectFlag,"1");
        queryWrapper.orderByDesc(AnswerTemplate::getCreateTime);
        AnswerTemplate first = CollectionUtil.getFirst(answerTemplateMapper.selectList(queryWrapper));
        if (first != null){
            return first;
        }else {
            LambdaQueryWrapper<AnswerTemplate> wrapper = new LambdaQueryWrapper<>();
            wrapper.orderByDesc(AnswerTemplate::getCreateTime);
            return CollectionUtil.getFirst(answerTemplateMapper.selectList(wrapper));
        }
    }
}
